package cn.sccl.dirms.topo.discovery.util;

import java.util.regex.Pattern;

/**
 * Created by liuchuan on 1/5/17.
 */
public class REUtil {

    public static boolean isMac(String mac) {
        if (mac == null || mac.equals("00:00:00:00:00:00")) {
            return false;
        }
        if (Pattern.compile(Constants.PatternMac).matcher(mac.toUpperCase()).find()) {
            return true;
        }
        return false;
    }

    public static String upperMac(String mac) {
        if (REUtil.isMac(mac)) {
            return mac.toUpperCase();
        } else {
            return null;
        }
    }

    public static boolean isIp(String ip) {
        if (ip == null) {
            return false;
        }
        if (Pattern.compile(Constants.PatternIp).matcher(ip.toUpperCase()).find()) {
            return true;
        }
        return false;
    }

    public static boolean isEffectiveIp(String ip) {
        if (REUtil.isIp(ip)) {
            if (ip.startsWith("127.") || ip.equals("0.0.0.0")
                    || ip.startsWith("224.0") || ip.endsWith(".0")
                    || ip.endsWith(".255")) {
                return false;
            }
            return true;
        }
        return false;
    }

    public static boolean isNumber(String portId) {
        if (Pattern.compile(Constants.Number).matcher(portId).find()) {
            return true;
        }
        return false;
    }
    public static String tenArrayString2HexStr(String bString, String split,String split2){
        String[] array = bString.split(split);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                buffer.append(split2);
            }
            String hex = Integer.toHexString(Integer.valueOf(array[i]));
            if(hex.length() ==1){
                hex = "0"+hex;
            }
            buffer.append(hex);
        }
        return buffer.toString().toUpperCase();
    }


    public static String binaryStringArray2hexStr(String bString, String split) {
        String[] array = bString.split(split);
        StringBuffer buffer = new StringBuffer();
        String splitStr = split.replace("\\", "");

        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                buffer.append(splitStr);
            }
            buffer.append(binaryString2hexString(array[i]));
        }
        return buffer.toString();
    }

    public static String binaryString2hexString(String bString) {
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)
            return null;
        StringBuffer tmp = new StringBuffer();
        int iTmp = 0;
        for (int i = 0; i < bString.length(); i += 4) {
            iTmp = 0;
            for (int j = 0; j < 4; j++) {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            tmp.append(Integer.toHexString(iTmp));
        }
        return tmp.toString();
    }

}
